/*DONE* Table [dbo].[<CompanyName>$Location$437dbf0e-84ff-417a-965d-ed2bb9650972]  1/2  Script Date: 28.7.2020 - JI ******/
/*Add prestaging table for all companies in LSInsight$Companies table*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyCounter as INT
DECLARE @i as INT

SET @i = 0
SET @CompanyCounter = (SELECT MAX([Company]) FROM LSInsight$Companies)

WHILE @i <= @CompanyCounter 
BEGIN
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @i)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Location$437dbf0e-84ff-417a-965d-ed2bb9650972]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Location$437dbf0e-84ff-417a-965d-ed2bb9650972](
	[timestamp] [timestamp] NOT NULL,
	[Code] [nvarchar](10) NOT NULL,
	[Name] [nvarchar](100) NOT NULL,
	[Default Bin Code] [nvarchar](20) NOT NULL,
	[Name 2] [nvarchar](50) NOT NULL,
	[Address] [nvarchar](100) NOT NULL,
	[Address 2] [nvarchar](50) NOT NULL,
	[City] [nvarchar](30) NOT NULL,
	[Phone No_] [nvarchar](30) NOT NULL,
	[Phone No_ 2] [nvarchar](30) NOT NULL,
	[Telex No_] [nvarchar](30) NOT NULL,
	[Fax No_] [nvarchar](30) NOT NULL,
	[Contact] [nvarchar](100) NOT NULL,
	[Post Code] [nvarchar](20) NOT NULL,
	[County] [nvarchar](30) NOT NULL,
	[E-Mail] [nvarchar](80) NOT NULL,
	[Home Page] [nvarchar](90) NOT NULL,
	[Country_Region Code] [nvarchar](10) NOT NULL,
	[Use As In-Transit] [tinyint] NOT NULL,
	[Require Put-away] [tinyint] NOT NULL,
	[Require Pick] [tinyint] NOT NULL,
	[Cross-Dock Due Date Calc_] [varchar](32) NOT NULL,
	[Use Cross-Docking] [tinyint] NOT NULL,
	[Require Receive] [tinyint] NOT NULL,
	[Require Shipment] [tinyint] NOT NULL,
	[Bin Mandatory] [tinyint] NOT NULL,
	[Directed Put-away and Pick] [tinyint] NOT NULL,
	[Default Bin Selection] [int] NOT NULL,
	[Outbound Whse_ Handling Time] [varchar](32) NOT NULL,
	[Inbound Whse_ Handling Time] [varchar](32) NOT NULL,
	[Put-away Template Code] [nvarchar](10) NOT NULL,
	[Use Put-away Worksheet] [tinyint] NOT NULL,
	[Pick According to FEFO] [tinyint] NOT NULL,
	[Allow Breakbulk] [tinyint] NOT NULL,
	[Bin Capacity Policy] [int] NOT NULL,
	[Open Shop Floor Bin Code] [nvarchar](20) NOT NULL,
	[To-Production Bin Code] [nvarchar](20) NOT NULL,
	[From-Production Bin Code] [nvarchar](20) NOT NULL,
	[Adjustment Bin Code] [nvarchar](20) NOT NULL,
	[Always Create Put-away Line] [tinyint] NOT NULL,
	[Always Create Pick Line] [tinyint] NOT NULL,
	[Special Equipment] [int] NOT NULL,
	[Receipt Bin Code] [nvarchar](20) NOT NULL,
	[Shipment Bin Code] [nvarchar](20) NOT NULL,
	[Cross-Dock Bin Code] [nvarchar](20) NOT NULL,
	[To-Assembly Bin Code] [nvarchar](20) NOT NULL,
	[From-Assembly Bin Code] [nvarchar](20) NOT NULL,
	[Asm_-to-Order Shpt_ Bin Code] [nvarchar](20) NOT NULL,
	[Base Calendar Code] [nvarchar](10) NOT NULL,
	[Use ADCS] [tinyint] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
	[$systemCreatedAt] [datetime] NOT NULL,
	[$systemCreatedBy] [uniqueidentifier] NOT NULL,
	[$systemModifiedAt] [datetime] NOT NULL,
	[$systemModifiedBy] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Location$437dbf0e-84ff-417a-965d-ed2bb9650972$Key1] PRIMARY KEY CLUSTERED 
(
	[Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Location$437dbf0e-84ff-417a-965d-ed2bb9650972$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END
SET @i = @i+1
END
